﻿using System;
using System.Drawing;
using System.Reflection;
using WinForm.Modules;

namespace WinForm.Common
{
    /// <summary>
    /// Function Module
    /// </summary>
    public class FunctionModule
    {
        #region 字段

        private readonly string _name;//模块名称
        private readonly string _description;//模块描述
        private readonly string _group;//所属的分组
        private readonly string _imageName;//图片名称
        private readonly Type _type;//该模块对应的用户控件的类型

        private ModuleBase _moduleControl;//该模块对应的用户控件实例

        #endregion 字段

        #region 属性

        /// <summary>
        /// 功能模块名称
        /// </summary>
        public string Name { get { return _name; } }

        /// <summary>
        /// 模块描述
        /// </summary>
        public string Description { get { return _description; } }

        /// <summary>
        /// 模块所属分组
        /// </summary>
        public string Group { get { return _group; } }

        /// <summary>
        /// 大图片
        /// </summary>
        public Image SmallImage { get { return ResourcesHelper.GetImageFromImagesFolder(_imageName, ImageSize.Small16); } }

        /// <summary>
        /// 小图片
        /// </summary>
        public Image LargeImage { get { return ResourcesHelper.GetImageFromImagesFolder(_imageName, ImageSize.Large32); } }
        
        /// <summary>
        /// 是否正在显示中
        /// </summary>
        public bool IsShown { get; set; }

        /// <summary>
        /// 该模块对应的用户控件的类型
        /// </summary>
        public Type Type { get { return _type; } }

        /// <summary>
        /// 模块对应的用户控件实例
        /// </summary>
        public ModuleBase UserControl
        {
            get
            {
                if (_moduleControl == null)
                {
                    ConstructorInfo constructorInfoObj = _type.GetConstructor(Type.EmptyTypes);//获取指定参数的构造函数，参考msdn
                    if (constructorInfoObj == null)
                        throw new ApplicationException(string.Format(ResourcesHelper.GetString("PublicConstructorError"), _type.FullName));
                    _moduleControl = constructorInfoObj.Invoke(null) as ModuleBase;
                }
                return _moduleControl;
            }
            set { _moduleControl = value; }
            
        }

        /// <summary>
        /// 模块控件是否创建
        /// </summary>
        public bool Created { get { return _moduleControl != null; } }

        /// <summary>
        /// 模块控件类型名
        /// </summary>
        public string TypeName
        {
            get
            {
                //if (Created)
                //    return TModule.GetType().Name;
                return _type.Name;
            }
        }

        #endregion 属性

        #region 构造与析构函数
        public FunctionModule(string name, string description, string group, string imageName, Type type)
        {
            // 如果传入的Type类型不是UserControl的子类，抛出异常
            if (!type.IsSubclassOf(typeof(ModuleBase)))
                throw new ArgumentException(ResourcesHelper.GetString("SubclassError"),"type");
            _name = name;
            _description = description;
            _group = group; 
            _imageName = imageName;
            _type = type;
            _moduleControl = null;
            
        }

        



        //~FunctionModule()
        //{
        //    ModuleDispose();
        //}


        //public void ModuleDispose()
        //{
        //    //if (_moduleControl != null && _moduleControl.AllowDispose)
        //    //{
        //    //    //todo:this.userControl.ResetPibbonPage();
        //    //    _moduleControl.Dispose();
        //    //    _moduleControl = null;
        //    //}
        //}
        #endregion 构造与析构函数


       
    }
}